<script src="/static/layui/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="/static/layui/lib/layui-v2.5.5/css/layui.css" media="screen"/>
<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>增加</legend>
        </fieldset>
        <form class="layui-form" action="" id="number_form">
            <div class="layui-inline">
                <label class="layui-form-label">添加库存</label>
                <div class="layui-input-inline">
                    <select name="item_id" id="selectstock" lay-verify="required" lay-search="">
                        <option value="">直接选择或搜索选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">购入价格</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入价格" class="layui-textarea" style="width:250px;height:10px" id="price"
                              name="price"
                              lay-verify="required"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="add_btn">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    function render_form(stock_info_obj) {
        for (var i = 0; i < stock_info_obj.length; i++) {
            $("#selectstock").append("<option value='" + stock_info_obj[i].id + "'>" + stock_info_obj[i].name + "</option>");
        }
        layui.use(['form'], function () {
            var form = layui.form;
            form.render();
        })
    }
    var stock_info = window.sessionStorage.getItem("stocks");
    var stock_info_obj = JSON.parse(stock_info)
    if (stock_info) {
        render_form(stock_info_obj)
    } else {
        $.ajax({
            type: 'GET',
            url: "/api/csgostock?type=1",
            dataType: "json",
            contentType: 'application/json',
            async: false,
            success: function (result) {
                if (result.success == "false") {
                    alert("添加失败!");
                } else if (result.exception == "true") {
                    alert("服务器异常");
                } else if (result.success == "true") {
                    render_form(result.data)
                    window.sessionStorage.setItem("stocks", JSON.stringify(result.data));
                }
            }, error: function (result) {
                console.log(result);
            }
        });
    }
    layui.use(['form'], function () {
        var form = layui.form
            , $ = layui.jquery;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();


        //监听提交
        form.on('submit(add_btn)', function (data) {
            var formObject = {};
            var formArray = $("#number_form").serializeArray();
            $.each(formArray, function (i, item) {
                formObject[item.name] = item.value;
            });
            formObject["type"] = 3;
            var data_obj = JSON.stringify(formObject);
            $.ajax({
                type: 'POST',
                url: "/api/csgostock",
                data: data_obj,
                dataType: "json",
                contentType: 'application/json',
                async: false,
                success: function (result) {
                    if (result.success == "false") {
                        alert("添加失败!");
                    } else if (result.exception == "true") {
                        alert("服务器异常");
                    } else if (result.success == "true") {
                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                }, error: function (result) {
                    console.log(result);
                }
            });
        });

    });


</script>